const API = require('../api/index')
const {
  typeJson
} = require('../../utils/typejson')
let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
const {
  get,
  post
} = require('../../request/index')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    type: 1, // 领导,0部门
    name: '新增',
    show1: false,
    formName: '',
    deptTreeList: [],
    show: false,
    show1: false,
    mainActiveIndex: 0,
    max: 1,
    usingUnit: [],
    show1: false,
    activeId: [],
    form: {
      checkPart: 1,
      mainProcess: "",
      laborSituation: "",
      safeDetail: "",
      checkResult: "",
      checkDetail: "",
      checkPic: "",
    },
    wuanlist: [], // 人员数据
    wuanlistCache: [], // 模糊搜索数据
    peopleValue: '', // 模糊搜索数据关键词
    result: [],
    resultint: [],
    resultVal: [],
    showPeople: false,
  },
  // 人员选择弹框
  selent() {
    this.setData({
      show1: true
    })
  },
  onClickNav({
    detail = {}
  }) {
    this.setData({
      mainActiveIndex: detail.index || 0,
    });
  },
  onClose() {
    this.setData({
      show: false,
      show1: false,
    });
  },
  onClickItem({
    detail = {}
  }) {
    console.log(detail);
    const activeId = this.data.activeId === detail.id ? null : detail.id;
    let value = 'form.planName'; // 注意这里哈
    this.setData({
      activeId,
      usingUnit: [],
      show1: false,
      [value]: detail.text
    });
    this.postbalstWork(activeId)

  },
  peopleBtn(name) {
    this.setData({
      showPeople: true,
      formName: name.target.dataset.name
    })
  },
  onClosePeople() {
    this.setData({
      showPeople: false
    })
  },
  onChangePeople(name) {
    let newListData = []; // 用于存放搜索出来数据的新数组
    if (name.detail) {
      this.data.wuanlist.forEach((item) => {
        if (item.nickName.indexOf(name.detail) !== -1) {
          console.log(item);
          newListData.push(item);
        }
      });
    }
    this.setData({
      wuanlistCache: newListData
    })
  },
  onChangegroup(event) {
    this.setData({
      result: event.detail,
    });
    console.log(event.detail);
  },
  toggle(event) {
    // console.log(event);
    console.log(event.detail);
    let list = this.data.result
    let lists = this.data.resultVal
    let join = ''
    let flag = -1
    const {
      name,
      text,
      listitem
    } = event.currentTarget.dataset;
    this.data.result.forEach((item, index) => {
      if (item === text) {
        flag = index
      }
    })
    if (flag != -1) {
      list.splice(flag, 1);
      lists.splice(flag, 1);
    } else {
      list.push(text)
      lists.push(listitem.userId)
    }
    list.length > 0 ? join = list.join(',') : join = '';
    let value = 'form.' + this.data.formName; // 注意这里哈
    this.setData({
      result: list,
      responUserId: list,
      [value]: join,
      resultVal: lists
    });
  },
  pClick(event) {
    const {
      index
    } = event.currentTarget.dataset;
    let list = this.data.result
    let lists = this.data.resultVal


    let value = 'form.checkPerson'; // 注意这里哈
    let join = ''
    list.splice(index, 1);
    lists.splice(index, 1);

    list.length > 0 ? join = list.join(',') : join = '';
    this.setData({
      result: list,
      responUserId: list,
      resultVal: lists,
      [value]: join
    });
  },
  changeTime1(e) {
    let value = 'form.startTime'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  changeTime2(e) {
    let value = 'form.endTime'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  changeUpint(e) {
    let value = 'form.trainPic'; // 注意这里哈
    this.setData({
      [value]: e.detail,
    });
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  async getDeptTree() {
    let res = await get('/system/user/deptTree');

    if (res.code == 200) {
      let newJsonParam = JSON.parse(JSON.stringify(res.data).replace(/label/g, "text"));
      this.setData({
        deptTreeList: newJsonParam
      })
    }
  },
  async postbalstWork() {
    let res = await get(`/system/user/list?pageNum=1&pageSize=999`)
    this.setData({
      wuanlist: res.rows
    })
  },
  submit() {
    let join = this.data.resultVal.join(',')
    let obj = Object.assign(this.data.form, {
      checkPerson: join,
      creatId: this.data.prolist.userId,
      type: this.data.type
    })
    post('/app/inspection/add', obj).then(res => {
      if (res.code == 200) {
        const pages = getCurrentPages()     //获取页面列表
        const perpage = pages[pages.length - 2]    //上一页   
        perpage.onLoad({
          type:1
        })
        wx.showToast({
          title: '提交成功',
          duration: 3000,
          icon: 'success',
          mask: true
        })
        wx.navigateBack({
          delta: 0,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDeptTree()
    this.postbalstWork()
    this.setData({
      type: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})